package com.groupbuying.redpacket.utils;



import com.groupbuying.redpacket.constants.SmsConstants;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author gxy
 * @Description 发送短信工具类
 * @Date 2019/5/27 10:06
 **/
public class SmsUtils {
    public static boolean sendSms(String mobile, String message) {

        boolean flag = false;
        try {
            // 创建StringBuffer对象用来操作字符串
            StringBuffer sb = new StringBuffer();//"http://m.5c.com.cn/api/send/?"
            sb.append(SmsConstants.SMS_SEND_URL);

            // APIKEY df4350849f314e39a1047565214f945f
            sb.append("apikey=").append(SmsConstants.SMS_APIKEY);

            // 用户名mengtai
            sb.append("&username=").append(SmsConstants.SMS_USERNAME);

            // 向StringBuffer追加密码 asdf1234q
            sb.append("&password=").append(SmsConstants.SMS_PASSWORD);
            // 向StringBuffer追加手机号码
            sb.append("&mobile=").append(mobile);

            // 向StringBuffer追加消息内容转URL标准码
            sb.append("&content=" +  URLEncoder.encode(SmsConstants.SMS_YD_SIGNATURE +message, SmsConstants.SMS_CONSTSNT_URLENCODER));

            // 创建url对象
            URL url = new URL(sb.toString());

            // 打开url连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置url请求方式 ‘get’ 或者 ‘post’
            connection.setRequestMethod("POST");
            // 发送
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

            // 返回发送结果
            String inputline = in.readLine();
            System.out.println("inputline=================="+inputline);
            if(!inputline.startsWith("success")){
                //throw new RuntimeException(inputline);
            }else{
                System.out.println(mobile+" 短信已发送");
                System.out.println("=====message========="+message);
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return flag;
        }
        return flag;
    }

    //验证码登录
    public static   boolean sendCodeForRegister(String mobile, String code){
        StringBuffer sb = new StringBuffer();
        sb.append("验证码：");
        sb.append(code);
        sb.append("，您正在通过手机验证码登录。60s内有效，请勿泄露。如非本人操作，请忽略本短信。");
        Map contentMap = new HashMap();
        contentMap.put("mobile",mobile);
        contentMap.put("content", sb.toString());
        return SmsUtils.sendSms(contentMap);
    }


    public static boolean sendSms(Map<String, String> params) {
        boolean flag = false;
        if(params.get("mobile") != null && !"".equals(params.get("mobile")) && params.get("mobile") != null && !"".equals(params.get("mobile"))){
            flag =  SmsUtils.sendSms(params.get("mobile"),params.get("content"));
        }
        return flag;
    }
}
